#include "spsc.h"

#include <thread>
#include <cstdint>
#include <iostream>

using namespace rigtorp;

constexpr int64_t MAXN = int64_t(1e8);
struct Timer {
	std::chrono::steady_clock::time_point start;
	Timer() : start(std::chrono::steady_clock::now()) {}
	~Timer() {
		auto finish = std::chrono::steady_clock::now();
		auto runtime = std::chrono::duration_cast<std::chrono::microseconds>(finish - start).count();
		std::cerr << runtime / 1e6 << "s" << std::endl;
	}
};


int main22222222() {
	Timer timer;
    // Fuzz and performance test 
    {
        const size_t iter = 100000;
        SPSCQueue<size_t> q(iter / 1000 + 1);
        std::atomic<bool> flag(false);
        std::thread producer([&] {
            while (!flag)
                ;
            for (size_t i = 0; i < iter; ++i) {
                q.emplace(i);
            }
        });

        size_t sum = 0;
        auto start = std::chrono::system_clock::now();
        flag = true;
        for (size_t i = 0; i < iter; ++i) {
            while (!q.front())
                ;
            sum += *q.front();
            q.pop();
        }
        auto end = std::chrono::system_clock::now();
        auto duration =
            std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);

        assert(q.front() == nullptr);
        assert(sum == iter * (iter - 1) / 2);

        producer.join();

        std::cout << duration.count() / iter << " ns/iter" << std::endl;
    }
	return 0;
}